# 存储和读取一维数据
def my_write():
    # 一维数据，可以使用列表，元组，或集合
    lst = ['张三', '李四', '王五', '赵六', '韩八']
    with open('student.csv', 'w') as file:
        file.write(', '.join(lst))    # 将列表转为字符串


def my_read():
    with open('student.csv', 'r') as file:
        s = file.read()
        lst = s.split(',')
        print(lst)


# 存储和读取二维数据
def my_write_table():
    lst = [
        ['名称', '单价', '数量'],
        ['苹果', '10', '11'],
        ['荔枝', '20', '22'],
        ['西瓜', '30', '33']
    ]
    with open('table.csv', 'w', encoding='utf-8') as file:
        for item in lst:
            line = ','.join(item)
            file.write(line)
            file.write('\n')


def my_read_table():
    data = []
    with open('table.csv', 'r', encoding='utf-8') as file:
        lst = file.readlines()
        # print(type(lst), lst)
        for item in lst:
            new_lst = item[:len(item)-1].split(',')
            data.append(new_lst)
    print(data)


if __name__ == '__main__':
    my_write()
    my_read()
    my_write_table()
    my_read_table()
